package com.szholly.data.general;

import com.szholly.utils.spring.SpringBeanFactory;
import com.szholly.utils.util.StringUtils;

/**
 * 定义字段信息类
 */
public class Field {
	
	public Field(){
		this.allowNull = true;
	}

	/**
	 * 字段名称
	 */
	private String fieldName;

	/**
	 * 字段ID(排序号)
	 */
	private int fieldID;

	/**
	 * 字段别名
	 */
	private String fieldAliasName;

	/**
	 * 字段类型
	 */
	private GlobalDataType dataType;

	/**
	 * 是否是自动增值列
	 */
	private boolean isAuto;

	/**
	 * 是否是主键
	 */
	private boolean isKey;

	/**
	 * 字段是否必需(注册时可忽略)
	 */
	private boolean isEssential;

	/**
	 * 长度
	 */
	private int maxLen;

	/**
	 * 小数位长度
	 */
	private int decimaldigits;

	/**
	 * 是否可以为空(默认可以为空)
	 */
	private boolean allowNull;

	/**
	 * 默认值
	 */
	private String defaultValue;
	
	/**
	 * 正则表达式
	 */
	private String regEx;

	/**
	 * 是否显示
	 */
	private boolean canShow;

	/**
	 * 是否只读
	 */
	private boolean readOnly;

	/**
	 * 唯一键分组
	 */
	private String uniqueGroup;

	/**
	 * 编码规则
	 */
	private String codeRule;

	/**
	 * 编码规则取值表达式
	 */
	private String codeRuleExp;

	/**
	 * 字典域
	 */
	private String codeTablePK;

	/**
	 * 字典域条件
	 */
	private String codeDomainName;

	/*
	 * 代码值获取脚本
	 */
	private String codeGetScript;

	/*
	 * 代码值设置脚本
	 */
	private String codeSetScript;

	/**
	 * 录入使用控件
	 */
	private String controlType;

	/**
	 * 字段单位
	 */
	private String unit;

	/**
	 * 字段分组
	 */
	private String group;

	/**
	 * 上下级分组
	 */
	private String relationGroup;

	/**
	 * 上下级索引
	 */
	private int relationIndex;

	/**
	 * 控件宽度
	 */
	private int controlWidth;

	/*
	 * 域值处理对象
	 */
	private IDomain domain;

	/**
	 * 是否是运算字段，默认为否(主键，外键，同步字段，计算相关字段)
	 */
	private boolean isCrucial;

	public String getFieldName() {
		return fieldName;
	}

	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

	public int getFieldID() {
		return fieldID;
	}

	public void setFieldID(int fieldID) {
		this.fieldID = fieldID;
	}

	public String getFieldAliasName() {
		if (StringUtils.IsNullOrSpace(fieldAliasName))
			return fieldName;
		else
			return fieldAliasName;
	}

	public void setFieldAliasName(String value) {
		fieldAliasName = value;
	}

	public GlobalDataType getDataType() {
		return dataType;
	}

	public void setDataType(GlobalDataType dataType) {
		this.dataType = dataType;
	}

	public boolean getIsAuto() {
		return isAuto;
	}

	public void setIsAuto(boolean value) {
		isAuto = value;
	}

	public boolean getIsKey() {
		return isKey;
	}

	public void setIsKey(boolean isKey) {
		this.isKey = isKey;
	}

	public int getMaxLen() {
		return maxLen;
	}

	public void setMaxLen(int maxLen) {
		this.maxLen = maxLen;
	}

	public int getDecimaldigits() {
		return decimaldigits;
	}

	public void setDecimaldigits(int decimaldigits) {
		this.decimaldigits = decimaldigits;
	}

	public boolean getAllowNull() {
		return allowNull;
	}

	public void setAllowNull(boolean allowNull) {
		this.allowNull = allowNull;
	}

	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	public String getRegEx() {
		return regEx;
	}

	public void setRegEx(String regEx) {
		this.regEx = regEx;
	}

	public Boolean getCanShow() {
		return canShow;
	}

	public void setCanShow(boolean canShow) {
		this.canShow = canShow;
	}

	public Boolean getReadOnly() {
		return readOnly;
	}

	public void setReadOnly(boolean readOnly) {
		this.readOnly = readOnly;
	}

	public String getUniqueGroup() {
		return uniqueGroup;
	}

	public void setUniqueGroup(String uniqueGroup) {
		this.uniqueGroup = uniqueGroup;
	}

	public String getCodeRule() {
		return codeRule;
	}

	public void setCodeRule(String value) {
		codeRule = value;
	}

	public String getCodeRuleExp() {
		return codeRuleExp;
	}

	public void setCodeRuleExp(String value) {
		codeRuleExp = value;
	}

	public String getControlType() {
		return controlType;
	}

	public void setControlType(String value) {
		controlType = value;
	}

	public String getCodeTablePK() {
		return codeTablePK;
	}

	public void setCodeTablePK(String value) {
		codeTablePK = value;
	}

	public String getCodeDomainName() {
		return codeDomainName;
	}

	public void setCodeDomainName(String value) {
		codeDomainName = value;
	}

	public String getUnit() {
		return unit;
	}

	public void setUnit(String value) {
		unit = value;
	}

	public String getGroup() {
		return group;
	}

	public void setGroup(String group) {
		this.group = group;
	}

	public boolean getIsEssential() {
		return isEssential;
	}

	public void setIsEssential(boolean value) {
		isEssential = value;
	}

	public boolean getIsCrucial() {
		return isCrucial;
	}

	public void setIsCrucial(boolean value) {
		isCrucial = value;
	}

	public String getRelationGroup() {
		return relationGroup;
	}

	public void setRelationGroup(String relationGroup) {
		this.relationGroup = relationGroup;
	}

	public int getRelationIndex() {
		return relationIndex;
	}

	public void setRelationIndex(int relationIndex) {
		this.relationIndex = relationIndex;
	}

	public int getControlWidth() {
		return controlWidth;
	}

	public void setControlWidth(int controlWidth) {
		this.controlWidth = controlWidth;
	}

	public String getCodeGetScript() {
		return codeGetScript;
	}

	public void setCodeGetScript(String value) {
		codeGetScript = value;
	}

	public String getCodeSetScript() {
		return codeSetScript;
	}

	public void setCodeSetScript(String value) {
		codeSetScript = value;
	}

	private boolean hasSysDomain = false;

	public boolean getHasDomain() {
		if (hasSysDomain) {
			return true;
		}
		return !StringUtils.IsNullOrSpace(this.codeTablePK);
	}

	public IDomain getDomain() {
		if (domain == null && this.getHasDomain()) {
			ITableFactory tableFactory = (ITableFactory) SpringBeanFactory
					.getBean("TableFactory");
			domain = tableFactory.openDomain(this.codeTablePK,
					this.codeDomainName);
		}
		return domain;
	}

	public void setDomain(IDomain domain) {
		this.domain = domain;
		hasSysDomain = true;
	}
}